Option Explicit

' 删除指定名称的menu(一级列表) 或 menu按钮
' 只关闭最外层的 menu + menu按钮即可
' @param menuName menu名称(一级列表名称) 或 menu按钮名称
Public Sub deleteMenu(ByRef menuName As String)
    Dim menu As CommandBarControl
    Set menu = Application.CommandBars("Worksheet Menu Bar").FindControl(Type:=msoControlPopup, tag:=menuName)

    If Not menu Is Nothing Then
        menu.Delete
    End If

    Set menu = Application.CommandBars("Worksheet Menu Bar").FindControl(Type:=msoControlButton, tag:=menuName)
    If Not menu Is Nothing Then
        menu.Delete
    End If
End Sub

'----------------------------------------
' 创建一个 menu
' @param menuName menu名称
' @return menu对象
Public Function getMenu(ByRef menuName As String) As CommandBarPopup
    Set getMenu = Application.CommandBars("Worksheet Menu Bar").FindControl(Type:=msoControlPopup, tag:=menuName)
    If Not getMenu Is Nothing Then
        getMenu.Delete
    End If

    Set getMenu = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
    With getMenu
        .Caption = menuName
        .tag = menuName
    End With
End Function

' 在一个父menu的基础上，在创建一个子menu
' @param parentMenu 父menu
' @param subMenuName 子menu名称
' @return 子menu对象
Public Function getSubMenu(ByRef parentMenu As CommandBarPopup, ByRef subMenuName As String) As CommandBarPopup
    Set getSubMenu = parentMenu.Controls.Add(Type:=msoControlPopup, Temporary:=True)
    ' With parentMenu.Controls.Add(Type:=msoControlPopup)
    With getSubMenu
        .Caption = subMenuName
        .tag = subMenuName
    End With
End Function

' 在指定的父menu上创建一个功能按钮
' @param parentMenu 父menu
' @param actionButtonName 功能按钮名
' @param actionModuleName 功能方法所在的 Module 名（必须是当前 xlsm/xla 内的 Module）
' @param actionMethodName 功能方法的方法名
Public Sub getActionButton(ByRef parentMenu As CommandBarPopup, ByRef actionButtonName As String, ByRef actionModuleName As String, ByRef actionMethodName As String)
    With parentMenu.Controls.Add(Type:=msoControlButton, Temporary:=True)
        .Caption = actionButtonName
        .tag = actionButtonName
        .OnAction = ThisWorkbook.Name & "!" & actionModuleName & "." & actionMethodName
    End With
End Sub

' 创建一个 menu 按钮（没有子列表，直接在 add-in 内创建一个按钮）
' @param actionButtonName 功能按钮名
' @param actionModuleName 功能方法所在的 Module 名（必须是当前 xlsm/xla 内的 Module）
' @param actionMethodName 功能方法的方法名
Public Sub getMenuButton(ByRef actionButtonName As String, ByRef actionModuleName As String, ByRef actionMethodName As String)
    Dim menuButton As CommandBarControl
    Set menuButton = Application.CommandBars("Worksheet Menu Bar").FindControl(Type:=msoControlButton, tag:=actionButtonName)
    If Not menuButton Is Nothing Then
        menuButton.Delete
    End If

    Set menuButton = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlButton, Temporary:=True)
    With menuButton
        .Caption = actionButtonName
        .tag = actionButtonName
        .Style = msoButtonCaption
        .OnAction = ThisWorkbook.Name & "!" & actionModuleName & "." & actionMethodName
    End With
End Sub